package com.lp.multi.thread.example03;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * @author: lp
 * @create: 2021-06-21 7:22
 * @description: 多线程下载图片
 */
public class DownLoadImageThread extends Thread{
    private String urlpath;
    private String downloadPath;

    public DownLoadImageThread(String urlpath,String downloadPath){
        this.urlpath = urlpath;
        this.downloadPath = downloadPath;
    }



    @Override
    public void run() {
        DownLoadUtil downLoadUtil = new DownLoadUtil();
        downLoadUtil.downLoad(urlpath,downloadPath);
        System.out.println("下载了文件"+downloadPath);
    }

    public static void main(String[] args) {
        DownLoadImageThread downLoadImageThread01 = new DownLoadImageThread("https://www.baidu.com/img/flexible/logo/pc/result.png", "result.png");
        DownLoadImageThread downLoadImageThread02 = new DownLoadImageThread("https://www.baidu.com/img/flexible/logo/pc/peak-result.png", "result2.png");
        DownLoadImageThread downLoadImageThread03 = new DownLoadImageThread("https://www.baidu.com/img/flexible/logo/pc/result@2.png", "result3.png");

        downLoadImageThread01.start();
        downLoadImageThread02.start();
        downLoadImageThread03.start();
    }
}

class DownLoadUtil{
    public void downLoad(String urlpath,String downloadPath){
        try {
            FileUtils.copyURLToFile(new URL(urlpath),new File(downloadPath));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常，下载失败!");
        }
    }
}
